package com.yiduan.apollocrm.views.mediators
{
	import com.yiduan.apollocrm.models.Campaign;
	import com.yiduan.apollocrm.views.CampaignEditView;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Label;
	
	public class CampaignEditMediator extends Mediator
	{
		[Inject]
		public var campaignEditView:CampaignEditView;
		
		public function CampaignEditMediator()
		{
			super();
		}
		
		override public function onRegister():void
		{
			updateView();
		}
		
		protected function updateView():void
		{
			if (this.campaignEditView.data == null)
			{
				this.campaignEditView.currentState = "New";
			}
			else
			{					
				var campaign:Campaign = this.campaignEditView.data as Campaign;
				this.campaignEditView.ownerNameInput.text = campaign.ownerName;
				this.campaignEditView.campaignNameInput.text = campaign.campaignName;			
				
				this.campaignEditView.statusComboBox.selectedItem =getRelatedStatusValue(campaign.status);
//				this.campaignEditView.typeComboBox.selectedItem = getRelatedTypeValue(campaign.type);
				this.campaignEditView.enableCheckbox.selected = campaign.enable;
				
				this.campaignEditView.expectedCostInput.text = campaign.expectedCost.toString();
				this.campaignEditView.actualCostInput.text = campaign.actualCost.toString();
				
				this.campaignEditView.startDate.selectedDate = getDateFromString(campaign.startDate);
//				this.campaignEditView.endDate.selectedDate = getDateFromString(campaign.endDate);

				this.campaignEditView.currentState = "Edit";	
			}
			
		}
		
		private function getDateFromString(ddMMYYYY:String):Date
		{			
			var date:Date = new Date(Number(ddMMYYYY.substring(4,8)),Number(ddMMYYYY.substring(2,4)),Number(ddMMYYYY.substring(0,2)));			
			return date;
		}
		
		private function getRelatedStatusValue(name:String):Object
		{
			
			var value:String = "";
			if (name == "无")
			{
				value="default";
			}
			else if (name == "计划中")
			{
				value="doing";
			}
			else if (name == "进行中")
			{
				value="planning";
			}
			else if (name == "完成")
			{
				value="done";
			}
			else if (name == "终止")
			{
				value="stopping";
			}
			var ob:Object = new Object();
			ob.Name = name;
			ob.Value= value
			return ob;
		}
		
		private function getRelatedTypeValue(name:String):Object
		{
			
			var value:String = "";
			if (name == "无")
			{
				value="default";
			}
			else if (name == "广告")
			{
				value="ad";
			}
			else if (name == "电子邮件")
			{
				value="email";
			}
			else if (name == "电话营销")
			{
				value="phone";
			}
						
			var ob:Object = new Object();
			ob.Name = name;
			ob.Value= value
			return ob;
		}
	}
}